package cn.online.taxi.verifycode.service.impl;

import cn.online.taxi.common.jedis.JedisClient;
import cn.online.taxi.common.utils.RedisUtil;
import cn.online.taxi.verifycode.service.VerifyCodeService;
import com.alibaba.dubbo.config.annotation.Service;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

/**
 * @Author BinBini
 * @Date 2021/3/28 9:31
 * @Version 1.0
 */
@Service
public class VerifyCodeServiceImpl implements VerifyCodeService {

    @Autowired
    private JedisClient jedisClient;
    @Value("120")
    private Integer PRE_EXPIRE;

    @Override
    public String generate(int identity, String phoneNumber) {
        String code = String.valueOf((int)((Math.random()*9+1)*Math.pow(10,5)));
        String keyPre = RedisUtil.generateKeyPreByIdentity(identity);
        String redisKey = keyPre + phoneNumber;
        jedisClient.set(redisKey, new Gson().toJson(code));
        jedisClient.expire(redisKey, PRE_EXPIRE);
        return code;
    }
}
